package com.jdy.haoduoaiteacher.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * match the width and keep the ratio at the same time.
 * 
 * http://stackoverflow.com/questions/4677269/how-to-stretch-three-images-across
 * -the-screen-preserving-aspect-ratio
 * 
 * @author zp @ 2016-1-28 上午9:48:14
 */
public class AspectRatioImageView extends ImageView {

  public AspectRatioImageView(Context context) {
    super(context);
  }

  public AspectRatioImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (getDrawable() != null) {
      int width = MeasureSpec.getSize(widthMeasureSpec);
      int height = width * getDrawable().getIntrinsicHeight()
          / getDrawable().getIntrinsicWidth();
      setMeasuredDimension(width, height);
    } else {
      super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }
  }
}